﻿using Common.Result;
using IService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.Dto.Dish;
using Model.Entitys;
using Service;

namespace TakeawayWebApi.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class DishController : ControllerBase
    {
        private readonly ILogger<DishController> _logger;
        private readonly IDishService _dishService;
        public DishController(ILogger<DishController> logger, IDishService dishService)
        {
            _logger = logger;
            _dishService = dishService;
        }
        /// <summary>
        /// 新增菜品
        /// </summary>
        /// <param name="dishAdd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Save(DishAdd dishAdd)
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            var res = await _dishService.SaveWithFlavorAsync(dishAdd, userId);
            if (res) return ResultHelper.Success("添加成功");
            return ResultHelper.Error("添加失败");
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet("page")]
        public async Task<ApiResult> GetPageInfo(int page, int pageSize, string? name)
        {
            var res = await _dishService.GetPageInfoAsync(page, pageSize, name);
            return ResultHelper.Success(res);
        }
        /// <summary>
        /// 根据id获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ApiResult> GetDishDtoByTd(long id)
        {
            return ResultHelper.Success(await _dishService.GetDishDtoByTdAsync(id));
        }
        /// <summary>
        /// 根据ids删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> DeleteDishDtoByTds(string ids)
        {
            //将ids通过‘，’拆分开合合并成list集合
            List<long> idList = ids.Split(',').Select(long.Parse).ToList();
            return ResultHelper.Success(await _dishService.DeleteDishByIdsAsync(idList));
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="dishAdd"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> EditDishDtoByTd(DishDto dishDto)
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            var res=await _dishService.EditDishByIdAsync(dishDto, userId);
            if(res) return ResultHelper.Success("修改成功");
            return ResultHelper.Error("修改失败");
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        [HttpPost("status/{status}")]
        public async Task<ApiResult> EditStatusByIds(string ids, int status)
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            //将ids通过‘，’拆分开合合并成list集合
            List<long> idList = ids.Split(',').Select(long.Parse).ToList();
            var res=await _dishService.EditStatusByIdsAsync(idList, status, userId);
            if (res) return ResultHelper.Success("修改成功");
            return ResultHelper.Error("修改失败");
        }

        [HttpGet("list")]
        public async Task<ApiResult> GetDishes([FromQuery]DishQuery dishQuery)
        {
            var res = await _dishService.GetDishesAsync(dishQuery);
            return ResultHelper.Success(res);
        }
    }
}
